package com.ktjy.ktmall.servlet;

import cn.hutool.core.lang.id.NanoId;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.WeChatPhoneInfo;
import com.ktjy.ktmall.entity.WxUser;
import com.ktjy.ktmall.service.WxUserService;
import com.ktjy.ktmall.service.impl.WxUserServiceImpl;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;

// 手机号一键登录
@WebServlet(name = "UserNameLoginServlet", urlPatterns = "/uni/userNameLogin")
public class UserNameLoginServlet extends HttpServlet {

    private String appId = null;   //小程序ID(appId)
    private String secret = null; // 小程序密钥
    private String accessTokenhttps = null; // 得到获取用户基本信息所需要的token网址
    private String webAccessTokenhttps = null; // 用于获取openId开发者id
    WxUserService wxUserService = null;

    // 微信支付的相关配置参数
    public void getConfigParam() {
        Properties params = new Properties();
        String configFile = "wxpay.properties";//配置文件路径
        //加载配置文件到输入流中
        InputStream is= UserNameLoginServlet.class.getClassLoader().getResourceAsStream(configFile);

        try {
            //从输入流中读取属性列表
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据指定的获取对应的值
        appId = params.getProperty("wx.appId");
        secret = params.getProperty("wx.secret");
        accessTokenhttps = params.getProperty("wx.accessTokenhttps");
        webAccessTokenhttps = params.getProperty("wx.webAccessTokenhttps");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        getConfigParam();
        wxUserService = new WxUserServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        // 手机号获取凭证
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");


        WxUser wxUser=wxUserService.findByUserName(userName,pwd);
        AjaxResult result;
        if(wxUser==null){
            result = AjaxResult.error("账号或密码错误！");
        }else{
            wxUser.setToken("Wx" + NanoId.randomNanoId(16));
            result = AjaxResult.success("登录成功", wxUser);
            out.print(JSONArray.toJSONString(result));
        }
        out.flush();
        out.close();
    }




    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
